//
//  RMMEvaluaView.m
//  Romomo
//
//  Created by puman on 2016/10/25.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMEvaluaView.h"

@implementation RMMEvaluaView
{
    UILabel *_scoreLabel;
    NSArray *_array;
    UILabel *_labelA;
    NSInteger _rowCount;
    NSMutableArray *_tipsArray;
    NSInteger _index;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kWhiteColor;
        CGFloat width = (kScreenWideth - 110) / 2;
        for (int i = 0; i < 2; i++) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15 + (width + 80) * i, kRow, width, 0.5)];
            view.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
            [self addSubview:view];
            if (i == 0) {
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(view.right, 0, 80, kMaric)];
                label.font = kFont(14);
                label.textAlignment = NSTextAlignmentCenter;
                label.textColor = COLORFROMCODE(0xB5B5B5, 1);
                label.text = @"评价技师";
                [self addSubview:label];
            }
        }
        for (int i = 0; i < 5; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width - 40 - 125) / 2  + 35 * i, kMaric + kRow, 25, 25)];
            imageView.image = [UIImage imageNamed:@"星1-2@3x"];
            imageView.tag = 20 +i;
            [self addSubview:imageView];
            if (i == 4) {
                _scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right + 5, imageView.top + kRow, kHalf, 15)];
                _scoreLabel.font = kFont(12);
                _scoreLabel.textColor = COLORFROMCODE(0xB5B5B5, 1);
                [self addSubview:_scoreLabel];
            }
        }
        _view = [[UIView alloc] initWithFrame:CGRectMake(0, 65, kScreenWideth, 115)];
        _view.backgroundColor = kWhiteColor;
        [self addSubview:_view];
        
        _labelA = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _view.width, 15)];
        _labelA.text = @"良好,手艺还需提高";
        _labelA.textAlignment = NSTextAlignmentCenter;
        _labelA.textColor = COLORFROMCODE(0xF4C001, 1);
        _labelA.font = kFont(12);
        [_view addSubview:_labelA];
        
        UILabel *lableB = [[UILabel alloc] initWithFrame:CGRectMake(15, _labelA.bottom + kRow, _view.width - 30, 15)];
        lableB.text = @"选择适合的标签";
        lableB.textColor = COLORFROMCODE(0x9B9B9B, 1);
        lableB.font = kFont(13);
        [_view addSubview:lableB];
        for (int i = 0; i < 20; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.layer.borderWidth = 1;
            button.tag = 10 + i;
            button.hidden = YES;
            button.layer.cornerRadius = 12.5;
            button.layer.borderColor = COLORFROMCODE(0xEEEEEE, 1).CGColor;
            [button setImage:[UIImage imageNamed:@"赞-2@3x"] forState:(UIControlStateNormal)];
            button.titleLabel.font = kFont(10);
            [button setTitleColor:COLORFROMCODE(0xB5B5B5, 1) forState:(UIControlStateNormal)];
            [_view addSubview:button];
        }
        
        _daShangBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _daShangBut.frame = CGRectMake(kScreenWideth / 2 - 80, 65 + kRow, 160, 36);
        [_daShangBut setTitleColor:COLORFROMCODE(0xF4C001, 1) forState:(UIControlStateNormal)];
        _daShangBut.titleLabel.font = kBFont(14);
        _daShangBut.layer.cornerRadius = 18;
        _daShangBut.layer.borderColor = COLORFROMCODE(0xF4C001, 1).CGColor;
        _daShangBut.layer.borderWidth = 1;
        [_daShangBut setTitle:@"打赏" forState:(UIControlStateNormal)];
        [self addSubview:_daShangBut];


    }
    return self;
}
- (void)normalStatusWith:(NSArray *)array andBegainIndex:(NSInteger)begianNndex{
    CGFloat width =  [self buttonTotalWidth:array];
    if (30 + (array.count - 1) * 10 + width <= self.width) {
        [self layoutButFramWith:array with:width index:begianNndex];
    } else {
        CGFloat widths = 15;
        NSInteger index = 0;
        NSMutableArray *titleArray = [NSMutableArray array];
        for (int i = 0; i < array.count; i++) {
            NSString *string = array[i];
            if (i == 0) {
                widths += [self titleLabelWidth:string];
            } else {
                widths += [self titleLabelWidth:string] + 10;
            }
            index = i;
            [titleArray addObject:string];
            if (widths > self.width) {
                break;
            }
        }
        [titleArray removeObjectAtIndex:index];
        [self layoutButFramWith:titleArray with:[self buttonTotalWidth:titleArray] index:begianNndex];
        for (int i = 0; i < index; i++) {
            [_tipsArray removeObjectAtIndex:0];
            _rowCount += 1;
        }
        begianNndex += 1;
        _index = begianNndex;
        [self normalStatusWith:_tipsArray andBegainIndex:begianNndex];
    }
}
- (void)layoutButFramWith:(NSArray *)array with:(CGFloat)width index:(NSInteger)begainIndex{
    CGFloat spacing = 0.0;
    if (array.count < 2) {
        spacing = 0.00;
    } else {
        spacing = (self.width - 30 - width) / (array.count - 1);
    }
    CGFloat x = 15;
    for (int i = 0; i < array.count; i++) {
        if ( i > 0) {
            x = x + [self titleLabelWidth:array[i - 1]] + spacing;
        }
        UIButton *button = [self viewWithTag:10 + i + _rowCount];
        button.frame = CGRectMake(x , _labelA.bottom + 30 + 35 * begainIndex, [self titleLabelWidth:array[i]], 25);
        button.hidden = NO;
//        if (_rowCount == 0) {
//            button.hidden = NO;
//        } else {
//            button.hidden = YES;
//        }
        [button setTitle:array[i] forState:(UIControlStateNormal)];
    }
}
- (CGFloat)buttonTotalWidth:(NSArray *)array{
    CGFloat width = 0.0;
    for (int i = 0; i < array.count; i++) {
        NSString *string = array[i];
        width += [self titleLabelWidth:string];
    }
    return width;
}
- (CGFloat)titleLabelWidth:(NSString *)string{
    return [UILabel backHeightWithString:string CGFloatCount:25 metho:WIDTH font:12] + 20;
}

- (void)setArray:(NSArray *)array{
    _rowCount = 0;
    _index = 0;
    _tipsArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        [_tipsArray addObject:[RMMFileManager retrunObjWith:dic[@"LABLE_NAME"]]];
    }
    [self normalStatusWith:_tipsArray andBegainIndex:0];
}
- (void)setStarCount:(NSNumber *)starCount{
    _scoreLabel.text = [NSString stringWithFormat:@"%.1f",[starCount integerValue] / 2.0];
    NSInteger count = [starCount integerValue];
    NSInteger allStar = count / 2;
    NSInteger halfStar = count % 2;
    for (int i = 0; i < 5; i++) {
        UIImageView *imageV = [self viewWithTag:20 + i];
        imageV.image = [UIImage imageNamed:@"星1-2@3x"];
        if (i < allStar) {
            imageV.image = [UIImage imageNamed:@"星-1@3x"];
        }
    }
    if (halfStar) {
        UIImageView *imageV = [self viewWithTag:20 + allStar];
        imageV.image = [UIImage imageNamed:@"星-3@3x"];
    }
    if (count <= 2) {
        _labelA.text = @"差,手艺急需提高";
        _labelA.textColor = COLORFROMCODE(0xF95B45, 1);
    } else if (count <= 6 && count > 2) {
        _labelA.text = @"一般,手还需提高";
        _labelA.textColor = COLORFROMCODE(0x2B2B2B, 1);
    } else if (count <= 8 && count > 6) {
        _labelA.text = @"良好,手艺很好";
        _labelA.textColor = kBasicColor;
    } else {
        _labelA.text = @"优秀,手艺非常好";
        _labelA.textColor = COLORFROMCODE(0xF4C001, 1);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
